package com.chen.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.annotation.OptLog;
import com.chen.common.OptTypeConst;
import com.chen.common.Result;
import com.chen.entity.Exam;
import com.chen.entity.Sign;
import com.chen.entity.StudentPaper;
import com.chen.entity.User;
import com.chen.exception.ServiceException;
import com.chen.mapper.StudentPaperMapper;
import com.chen.service.impl.StudentPaperServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author personcoder007
 * @since 2022-09-19
 */
@RestController
@RequestMapping("/studentPaper")
@Api(value = "StudentPaperController", tags = "学生考卷")
public class StudentPaperController {


    @Autowired
    private StudentPaperServiceImpl studentPaperService;

    @RequestMapping(value = "/findPage")
    @ApiOperation(value = "mybatis分页")
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "5") Integer pageSize) {

        QueryWrapper<StudentPaper> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("id");

        Page<StudentPaper> page = studentPaperService.page(new Page<StudentPaper>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    //根据id进行删除
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    @OptLog(optType = OptTypeConst.REMOVE)
    public Result deleteDict(@PathVariable("id") int id) {
        studentPaperService.removeById(id);
        return Result.success();
    }

    //批量删除
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除")
    @OptLog(optType = OptTypeConst.REMOVE_BATH)
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        studentPaperService.removeByIds(ids);

        return Result.success();
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增或者更新")
    @OptLog(optType = OptTypeConst.SAVE_OR_UPDATE)
    public Result addDict(@RequestBody StudentPaper studentPaper) {

        if (studentPaper.getId() == null) {
            List<StudentPaper> studentPaperList = studentPaperService.list(new QueryWrapper<StudentPaper>().eq("exam_id", studentPaper.getExamId()).eq("user_id", StpUtil.getLoginIdAsInt()));
            if (CollUtil.isNotEmpty(studentPaperList)) {
                throw new ServiceException(-1, "您已经提交了");
            }
            studentPaper.setTime(new Date());
            studentPaper.setUserId(StpUtil.getLoginIdAsInt());
        }


        studentPaperService.saveOrUpdate(studentPaper);

        return Result.success();
    }


    @GetMapping("/selectStudentPaperById/{id}")
    @ApiOperation(value = "根据id进行查询")
    public Result selectStudentPaperById(@PathVariable("id") Integer id) {
        return Result.success(studentPaperService.getById(id));
    }

    @GetMapping("/selectIsDoPaper/{examId}")
    @ApiOperation("查询是否已做过试卷")
    public  Result selectIsDoPaper(@PathVariable Integer examId){
        StudentPaper one = studentPaperService.getOne(new QueryWrapper<StudentPaper>().eq("exam_id", examId).eq("user_id", StpUtil.getLoginIdAsInt()));
        if (one!=null){
            return Result.success(401,"已经提交过了，请等待出成绩");
        }else {
            return Result.success();
        }
    }

}

